package com.example.bigevent.service.impl;

import ch.qos.logback.core.util.MD5Util;
import com.example.bigevent.common.constant.MessageConstant;
import com.example.bigevent.common.exception.FindByUsernameError;
import com.example.bigevent.common.exception.RegisterUserExistError;
import com.example.bigevent.common.exception.UserServiceException;
import com.example.bigevent.common.utils.Md5Util;
import com.example.bigevent.mapper.UserMapper;
import com.example.bigevent.pojo.entity.User;
import com.example.bigevent.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

@Service
public class UserServiceImpl  implements UserService {
    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper){
        this.userMapper=userMapper;
    }

    /**
     * 根据Username查找用户
     *
     * @param username 用户名
     * @return 返回User数据集
     */
    @Override
    public User findByUsername(String username) {
        if(username==null || username.equals(" ")){
            throw new FindByUsernameError(MessageConstant.FIND_BY_USERNAME_NULL);
        }
        return userMapper.selectByUsername(username);
    }

    /**
     * 注册用户账户
     *
     * @param username 用户名
     * @param password 密码
     */
    @Override
    public void register(String username, String password) {
        //加密
        String md5String= Md5Util.getMD5String(password);
        //添加记录
        userMapper.inset(username,md5String);
    }
    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 密码
     */
    @Override
    public void login(String username, String password) {
        //加密
        String md5String= Md5Util.getMD5String(password);
        if(!userMapper.selectByUsername(username).getPassword().equals(md5String)){
            throw new UserServiceException(MessageConstant.PASSWORD_ERROR);
        }
    }
}
